package it.antonio.ilpizze.servlet.render;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class JsonObject extends Json {

	private Map<String, Json> fields = new LinkedHashMap<String, Json>();

	
	public void put(final String key, Object value) {
		Json jsonValue = Json.convert(value);
		fields.put(key, jsonValue);
	}

	@Override
	public String toJson() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{");
		Iterator<Entry<String, Json>> it = fields.entrySet().iterator();

		while (it.hasNext()) {
			Entry<String, Json> entry = it.next();
			buffer.append("\"");
			buffer.append(entry.getKey());
			buffer.append("\":");
			buffer.append(entry.getValue().toJson());
			if (it.hasNext()) {
				buffer.append(",");
			}
		}
		buffer.append("}");
		return buffer.toString();
	}

}
